// pages/moreservice/shared_parking/shared_parking.js
const app = getApp()
var address = require('./city.js')

Page({
  setTabIndex(e) {
    console.log(e)
    let activeIndex = e.currentTarget.dataset.i;
    console.log(activeIndex);
    this.setData({
      activeIndex
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    activeIndex: 0,
    //级联器
    // 省市区三级联动初始化
    region: ["北京市", "北京市", "东城区"],
    // 级联器
  },
  ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      // title: '召唤师',
      content: '确定要删除这张图片吗？',
      cancelText: '再想想',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  publish: function () {
    wx.showToast({
      title: '发布成功！即将跳转',
      duration: 2000,
      success: function () {
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/moreservice/shared_parking/shared_parking',
          })
        }, 2000)
      }

    })

  },
  formsubmit: function (option) {
    // const parkaddress = parkaddress;
    const parkaddress = option.detail.value.address;
    // const parkaddress = e.detail.value.region;
    const parkownerdescription = option.detail.value.description;
    const parkrentmoney = option.detail.value.price;
    const parkownertel = option.detail.value.phone;
    const path = option.detail.target.dataset.path;
    const salernickname = option.detail.target.dataset.nickname;
    const salergender = option.detail.target.dataset.gender;
    const saleravatarimg = option.detail.target.dataset.avatarurl;
    wx.request({
      url: 'http://localhost:8080/ipark/parkshare/save',
      data: {
        path,
        parkrentmoney,
        parkaddress,
        parkownertel,
        parkownerdescription,
        salernickname,
        salergender,
        saleravatarimg,
      },
      header: {
        'Content-Type': 'application/json;charset=utf-8',
      },
      method: 'POST',
      success: (result) => {
      }
    })
    wx.uploadFile({
      // url: 'http://localhost:8080/ipark/parkshare/upload',
      url: 'url',
      filePath: this.data.imgList[0],
      name: 'file',
      formData: {
        'user': 'zjj'
      },
      success: function (res) {

      }
    })
  },
  formbuyersubmit: function (option) {
    const parkDemand = option.detail.value.description;
    const hopePrice = option.detail.value.price;
    const buyerTel = option.detail.value.phone;
    const buyerNickName = option.detail.target.dataset.nickname;
    const buyerGender = option.detail.target.dataset.gender;
    const buyerAvatarImg = option.detail.target.dataset.avatarurl;
    wx.request({
      url: 'http://localhost:8080/ipark/parksharebuyer/save',
      data: {
        parkDemand,
        hopePrice,
        buyerTel,
        buyerAvatarImg,
        buyerNickName,
        buyerGender,
      },
      header: {
        'Content-Type': 'application/json;charset=utf-8',
      },
      method: 'POST',
      success: (result) => {
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //级联器


    //级联器

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //级联器
  toCascader: function () {
    wx.navigateTo({
      url: '/pages/moreservice/shared_parking/addressCascader/addressCascader',
    })
  },
  // 选择省市区函数
  changeRegin(e) {
    this.setData({ region: e.detail.value });
    console.log(region)
  },
  //级联器
})